<?php
/*
 +----------------------------------------------------------------------
 + Title        : Upload 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2021/8/3 23:19
 + Last-time    : 2021/8/3 23:19+ chen2
 + Desc         : Upload
 +----------------------------------------------------------------------
*/


namespace app\api\controller\common;

use app\BaseController;
use randy\tools\ali\AliOss;
use randy\tools\units\Snowflake;
use think\Exception;
use think\facade\Config;
use think\facade\Env;
use think\facade\Filesystem;

class UploadController extends BaseController
{
    protected $middleware = [];

    /**
     * @throws Exception
     */
    public function save()
    {
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = Filesystem::putFile('upload', $file);
            $config = Filesystem::getDiskConfig('local');
            if ($info) {
                // 成功上传后 获取上传信息
                $info = $config['root'] . '/' . $info;
                //将\ 转换成 /
                $filePath = str_replace('\\', '/', $info);
                $photo = config('app.alioss.file_path') . "/upload" . '/' . Snowflake::get() . '.' . $file->extension();
                try {
                    $oss = new AliOss();
                    $oss_config = Config::get('app.alioss');
                    $oss->uploadOss($info, $photo);
                    $res['full_url'] = $oss_config['url']. $photo;
                    $res['path'] = $photo;
                    $res['name'] = $file->getOriginalName();
                    return $this->returnSuccess($res);
                } catch (\Exception $e) {
                    @unlink($filePath);
                    throw $e;
                }
            } else {
                error_abort('文件上传失败');
            }
        } else {
            error_abort('上传文件不存在');
        }
    }

    public function saveEditor()
    {
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = Filesystem::putFile('upload', $file);
            $config = Filesystem::getDiskConfig('local');
            if ($info) {
                // 成功上传后 获取上传信息
                $info = $config['root'] . '/' . $info;
                //将\ 转换成 /
                $filePath = str_replace('\\', '/', $info);
                $photo = config('app.alioss.file_path') . "/upload" . '/' . Snowflake::get() . '.' . $file->extension();
                try {
                    $oss = new AliOss();
                    $url = $oss->uploadOss($info, $photo);
                    return json(['location' => $url]);
                    //                    return $this->returnSuccess(['result' => $url]);
                } catch (Exception $e) {
                    @unlink($filePath);
                    throw $e;
                }
            } else {
                error_abort('文件上传失败');
            }
        } else {
            error_abort('上传文件不存在');
        }
    }

    public function appValidateToken() : bool
    {
        return true;
    }
}